package com.xunqi.ad.vo;

import com.xunqi.ad.enums.ResponseEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.validation.BindingResult;

/**
 * @Description:
 * @Created with IntelliJ IDEA.
 * @author: 夏沫止水
 * @createTime: 2020-04-30 16:57
 **/
@Data
/*@JsonInclude(value = JsonInclude.Include.NON_NULL)*/
@NoArgsConstructor
@AllArgsConstructor
public class ResponseVo <T> {

    private Integer code;

    private String message;

    private T data;

    public ResponseVo(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResponseVo(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    /**
     * 成功返回的方法
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResponseVo<T> success(T data) {
        return new ResponseVo<T>(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getMsg(),data);
    }

    public static <T> ResponseVo<T> success() {
        return new ResponseVo<T>(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getMsg());
    }


    /**
     * 失败返回的方法
     * @param responseEnum
     * @param <T>
     * @return
     */
    public static <T> ResponseVo<T> error(ResponseEnum responseEnum) {
        return new ResponseVo<T>(responseEnum.getCode(),responseEnum.getMsg());
    }
    public static <T> ResponseVo<T> error(ResponseEnum responseEnum,String msg) {
        return new ResponseVo<T>(responseEnum.getCode(),msg);
    }
    public static <T> ResponseVo<T> error(ResponseEnum responseEnum, BindingResult bindingResult) {
        return new ResponseVo<>(responseEnum.getCode(),bindingResult.getFieldError().getDefaultMessage());
    }

}
